﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NearForums.Validation;
using System.Text.RegularExpressions;

namespace NearForums
{
    public class Questions:Entity, IAccessRightContainer
    {
        public Questions()
        {
            Answers = new List<Answers>();
            PostAccessRole = UserRole.Member;
        }

        public Questions(int id):this()
        {
            this.Id = id;
        }

        public int Id
        {
            get;
            set;
        }

        [RequireField]
        public string Name
        {
            set;
            get;
        }
        
        
        [RequireField]
        [Length(50)]
        public string Title
        {
            set;
            get;
        }

        [Length(500)]
        public string Description
        {
            get;
            set;
        }

        public List<Answers> Answers
        {
            get;
            set;
        }

        [RequireField]
        [RegexFormat(@"^\s*([\w\-\.]+(\s+|$)){1,6}$", RegexOptions.IgnoreCase)]
        public TagList Tags
        {
            get;
            set;
        }

        public int ViewCount
        {
            get;
            set;
        }

        public int Voted
        {
            get;
            set;
        }

        public DateTime PublishDate
        {
            get;
            set;
        }

        public Forum Forum
        {
            get;
            set;
        }
        
        [RequireField]
        public User User
        {
            get;
            set;
        }
        #region IAccessRightContainer Members
        public UserRole? ReadAccessRole
        {
            get;
            set;
        }

        [PostGreaterThanReadRights]
        public UserRole PostAccessRole
        {
            get;
            set;
        }

        public bool HasPostAccess(UserRole? userRole)
        {
            if (userRole >= PostAccessRole)
            {
                return true;
            }
            return false;
        }

        public bool HasReadAccess(UserRole? userRole)
        {
            if (ReadAccessRole == null || userRole >= ReadAccessRole)
            {
                return true;
            }
            return false;
        }

        #endregion
    }
}
